

$(function(){
	Page.search();
});

var Page = {};
Page.search = function() {
	var url = urls.server + '/bikes';

	$.ajax({
        type: 'GET',
        async: true,
        url: url,
        dataType: 'json',
        data: {
            limit: 100,
            offset: 0
        },
        success: function(e){
        	if(e.length > 0) {
        		Page.showData(e);
        	}
        	else {
        		$('.bikes-data-list').html('<div class="error">未查询到数据</div>');
        	}
        }
    });
}

Page.showData = function(datas) {
	var h = '';
	for( var i=0, len=datas.length; i<len; i++ ) {
		var item = datas[i];

		h += '<div class="line-wrapper" data-id="'+ item.id +'">';
		h += '		<a href="../zongshu?key='+item.id+'">';
        var img = item.image_url ? item.image_url : '..\/imgs\/404.jpg';
        // h += '          <div class="moto-img" style="background-image: url(../imgs/404.jpg);"></div>';
        h += '          <div class="moto-img" style="background-image: url('+ img + ');"></div>';
        h += '          <div class="moto-content">';
        h += '              <div class="title">'+ item.year + '款' + item.brand + ' ' + item.name +'</div>';
        h += '              <div class="param">';
        var price = Number(item.price)/10000;
        price = parseFloat(price.toFixed(2));
        /*if(price.match('.0000')) {
            price = price.replace('.0000', '');
        }*/
        h += '                  <span class="price">'+ price +'万元</span>';
        h += '              </div>';
		h += '			    <div class="param">';

        var info = item.site + '/';
        var time = Page.getTimeDif(item.create_time);
        info += time + '/' + item.real_km + '/商家 ' + item.merchant_num + '/' + (item.sale_state == 1 ? '在售' : '已售');

		h += '				    <span class="text">'+ info +'</span>';
        h += '              </div>';
		h += '			</div>';
		// h += '			<div class="param">';
		// h += '				<span class="price">'+ (item.price/10000) +'万</span>';
		// h += '			</div>';
		// h += '			<div class="param">';
		// h += '				<span class="lbl">'+ item.year + '/' + item.real_km +'</span>';
		// h += '			</div>';
		h += '		  </a>';
		// h += '		<div class="line-function">';
		// h += '			<a class="btn btn-edit" href="../modify?key='+ item.id +'">编辑</a>';
		// h += '			<a class="btn btn-imgs" href="../tupian?key='+ item.id +'">图片管理</a>';
		// h += '			<a class="btn btn-delete" data-id="'+ item.id +'" data-name="'+ item.name +'" href="javascript:;">删除</a>';
		// h += '		</div>';
		h += '</div>';
	}

	h += '<div class="error">没有更多数据了</div>'

	$('.bikes-data-list').html(h);


    $('.btn-delete').click(Page.openRmoveModal);
}
Page.getTimeDif = function(time) {
    var date1 = new Date(time);  

    var date2 = new Date(); 
    var dif = date2.getTime() - date1.getTime(); //ms
    var mins = Math.floor(dif/(1000*60)); //min
    if( mins < 60 ) {
        return mins + '分钟前发布';
    }
    var hours =  Math.floor(mins/60);
    if(hours < 24 ) {
        return hours + '小时前发布';
    }
    var days = Math.floor(hours/24);
    return days + '天前发布';
}

Page.openRmoveModal = function(){
    var me = $(this);
    $('.alert-hint').html('确定删除车辆：' + me.attr('data-name') + ' ?删除后不可恢复');
    $('.btn-alert-sure').unbind('click').click(Page.remove).attr({'data-id': me.attr('data-id')});
    $('.modal-alert').modal('show');
}
Page.remove = function() {
    var me = $(this);
    var url = urls.server + '/bikes/' + me.attr('data-id');

    $.ajax({
        type: 'delete',
        async: true,
        url: url,
        data: {
            access_token: 'AABBCCDD'
        },
        dataType: 'json',
        success: function(e){
            if(e && e.action == 'success') {
                $('.modal-alert').modal('hide');
                $('.line-wrapper[data-id="'+ me.attr('data-id') +'"]').remove();
            }
        }
    });
}


